package org.itboys.mobile.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayActivity;
import org.itboys.mobile.entity.mongo.SundayCollection;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/7/10
 * @Description：web端活动管理
 */
@Service
public class SundayActivityService extends BaseMongoService<SundayActivity> {

    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }
    @Override
    protected Class<SundayActivity> getEntityClass() {
        return SundayActivity.class;
    }
    @Autowired
    private SundayUploadService uploadService;

    @Autowired
    private SundayCollectionService collectionService;

    /**
    *@Author:jiaqilei
    *@Date:11:31 2018/7/10
     *@Param id
    *@Discription:根绝id获取一个活动实体
    */
    public SundayActivity findOne(Long id){
        return super.getById(id);
    }
    /**
    *@Author:jiaqilei
    *@Date:11:34 2018/7/10
     *@Param activity
    *@Discription:更新或修改活动信息
    */
    public void save(Long id,SundayActivity activity,MultipartFile imageFile,MultipartFile backImageFile,MultipartFile headImageFile)throws Exception{
        //保存主图
        Map<String, String> map =uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(map.get(SundayUploadService.UPLOAD_HALF_PATH))){
            activity.setImage(map.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        Map<String, String> map1 =uploadService.uploadSingle(backImageFile);
        if(StringUtils.isNotEmpty(map1.get(SundayUploadService.UPLOAD_HALF_PATH))){
            activity.setBackImage(map1.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        Map<String, String> map2 =uploadService.uploadSingle(headImageFile);
        if(StringUtils.isNotEmpty(map2.get(SundayUploadService.UPLOAD_HALF_PATH))){
            activity.setHeadImage(map2.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        if(LongUtil.isNotZreo(id)){
            super.updateExceptEmpty(id,activity);
        }else {
            activity.setIsRecommend(SundayCommonConstants.NO);
            super.save(activity);
        }
    }

    /**
     * web端查询所有活动信息
     * @param request
     * @return
     */
    public PageResult<SundayActivity> select(HttpServletRequest request){
        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request, "name");
        Map<String,Object> param=new HashMap<>();
        param.put("isDeleted", SundayCommonConstants.NO);
        PageResult<SundayActivity> sundayActivitys = super.containpageQuery(request, vagueParam, param);
        if(!ListUtil.isNotNull(sundayActivitys.getData())){
            return sundayActivitys;
        }
        List<Long> ids=new ArrayList<>();
        for (SundayActivity activity:sundayActivitys.getData()
             ) {
            ids.add(activity.getId());
        }
        List<SundayCollection> collections = collectionService.select(ids,SundayCommonConstants.collect_type_activity);
       /**
         * 获取活动被收藏次数、热度
         */
        for (SundayActivity activity:sundayActivitys.getData()){
            int collectionNum=0;
            for (SundayCollection collection:collections
                 ) {
                if(collection.getOtherId().equals(activity.getId())){
                    collectionNum+=1;
                }
            }
            activity.setHot(collectionNum);
        }
        return sundayActivitys;
    }

    /**
     * 修改活动状态
     * @param id
     * @param starus
     */
    public void change(Long id,Integer starus){
        super.update(id,"status",starus);
    }

    /**
     * 修改首页推荐
     * @param id
     * @param starus
     */
    public void setRecommend(Long id,Integer starus){
        super.update(id,"isRecommend",starus);
    }
}
